using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace BizTalkCrossReferenceManager.Data
{
    /// <summary>
    /// Provides access to cross tab data
    /// </summary>
    public sealed class CrossTab
    {
        /// <summary>
        /// Gets the cross tab data for a specific xref type
        /// </summary>
        /// <param name="xRefType"></param>
        /// <returns></returns>
        public static DataSet GetCrossTabData(string xRefType)
        {
            DataSet ds = new DataSet();
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BizTalkCrossReferenceManager.Properties.Settings.CrossReferenceDataConnectionString"].ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("GetXRefIdCrossTab", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@idXRef", xRefType);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {                        
                        ds.Tables.Add(new DataTable("Results"));
                        ds.Load(rdr, LoadOption.OverwriteChanges, ds.Tables[0]);
                        foreach (DataColumn dataCol in ds.Tables[0].Columns)
                        {
                            if (dataCol.Ordinal > 0)
                                dataCol.ReadOnly = false;
                        }                        
                    }
                }
                conn.Close();
            }
            return ds;
        }
    }
}
